<?php
namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\model\AdminGroup as AdminGroupModel;

class Base extends \think\Controller
{
    public function _initialize()
    {
        $this->dbTablePrefix = \think\Config::get('database.prefix');

        //左边导航栏
        $controller = $this->request->controller();
        $action = $this->request->action();

        $this->assign('menuController',$controller);
        //$this->assign('menuAction',$action);

        //域名设置
        $domain = $_SERVER['HTTP_HOST'];// . ':' . $_SERVER['SERVER_PORT'];
        $this->assign('domain', $domain);

        $s_username = \think\Session::get('username');
        $s_menu_id = \think\Session::get('menu_id');
        $c_username = \think\Cookie::get('username');

        $admin_id = \think\Cookie::get('admin_id');

        $server = $this->request->server();

        if (empty($s_username) && empty($c_username))
        {
            // $this->error('请您登录后操作...', '/admin/login/index');
            return $this->redirect('/admin/login/index');
        } else if(empty($s_username)) {
            \think\Session::set('username', $c_username);
        }

        //存储管理员身份
        $menu_data = \app\admin\model\Menu::menuData();
        if(empty($s_menu_id)){
            $record = AdminModel::field('admin_group_id,password')->where('username' , $s_username)->find();
            $s_menu_id = AdminGroupModel::where('id' , $record['admin_group_id'])->value('menu_id');
        }
        $pieces = explode(",", $s_menu_id);

        foreach ($menu_data as $k=>$v){
            if(!empty($v["child"])){
                foreach ($v["child"] as $a=>$b){
                    if(!(in_array($b["id"],$pieces)) ){/*&& ($b["id"] != 28) && ($b["id"] != 29)*/
                        unset($menu_data[$k]["child"][$a]);
                    }
                }
            }
        }

        foreach ($menu_data as $k=>$v){
            if((empty($v["child"])) && ($v["id"] != 1)  ){/*&& ($v["id"] != 10)*/
                unset($menu_data[$k]);
            }
        }

        $this->assign('username',$s_username);
        $this->assign('menu_data', $menu_data);
    }
}